var express = require('express'),
	resource = require('express-resource'),
	fs = require('fs'),
	passport = require('passport'),
	auth = require('./config/middlewares/authorization'),
	pool = require('./database'),
	later = require('later'),
	mail = require("nodemailer").mail,
	http = require('http');

var app = express();

var config = require('./config/config');

//用户权限中间件
var notify = require('./config/middlewares/notify');

//数据权限中间件
var dataAuth = require('./config/middlewares/dataAuth');

//引入配置文件、环境设置
var setting = require('./config/setting')(app, config, passport, auth);

var employee = require('./app/model/employee');

// var models_path = config.root + '/app/model';
// fs.readdirSync(models_path).forEach(function(file) {
// 	require(models_path + '/' + file);
// });

// //用户权限中间件
// var notify = require('./config/middlewares/notify');

// var notifyApi = require('./config/middlewares/notifyApi');

//创建连接
pool.creatConnection(config, function(connection) {
	require('./config/router')(app, passport, notify, dataAuth, connection, auth);
	require('./config/passport')(passport, config, connection);

	// 邮件自动提醒
	later.date.localTime();
	var sched = later.parse.recur().on(23,26).dayOfMonth().on(8).hour(),//.on(38).minute()
		t = later.setInterval(function() {
			email();
		}, sched);

	var img = fs.readFileSync(config.root + "/public/assets/images/foss.png"); //导入图片  
	  
	var attachment_list = [  
	{  
	    'filename': 'attachment1.txt', //这里只是给附件取名称，而不是导入文件内容  
	    'contents': 'contents for attachment1.txt' //引入附件  
	},
	{  
	    'filename': 'аттачмент2.bin',  
	    'contents': new Buffer('binary contents', 'binary')
	}, 
	{  
	    'filename': "foss.png",  
	    'contents': img //导入图片文件  
	}  
	  
	];
	var cid_value = Date.now() + '.image.jpg';  
	  					  
	var attachments = [{  
	  
	    filename: 'foss.png',  
	  
	    contents: img,  
	  
	    cid: cid_value  
	  
	}]; 
	function email() {
	    var sendMsg = "<br/>&nbsp;各位同事：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;绩效考核期为每个月的<font color=red>23-26</font>号，烦请及时填写、考核及确认。</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：目前人事管理系统只限于人事行政、PMO、财务、总经理办公室人员试用<br/><br/>&nbsp;<div><span><div style='font-size: 10.0pt;font-family: verdana;margin: 10.0px;'><div><table class='MsoNormalTable' style='border-collapse: collapse;margin-left: 5.4pt;' cellspacing='0' cellpadding='0' border='0'><tbody><tr style='height: 68.75pt;'><td style='height: 68.75pt;width: 116.6pt;padding-bottom: 0.0cm;padding-top: 0.0cm;padding-left: 0.0cm;padding-right: 0.0cm;' valign='top' width='155'><p class='MsoNormal' style='margin: 0.5cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;font-family: arial , sans-serif;color: black;'><img src='cid:" + cid_value + "' /></span></b></p></td><td style='height: 68.75pt;width: 291.75pt;padding-bottom: 0.0cm;padding-top: 0.0cm;padding-left: 5.4pt;padding-right: 5.4pt;' valign='top' width='389'><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><span style='font-family: times new roman , serif;color: navy;'>&nbsp;</span></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;color: gray;'>广州人事行政部</span></b></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;color: gray;'>恒拓开源<span>(</span>天津<span>)</span>信息科技有限公司</span></b><span style='color: navy;'></span></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;color: gray;'>Forever Open Source Software INC.</span></b><span style='color: navy;'></span></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;color: gray;'>广州市白云区云霄路<span>89</span>号云霄大厦二楼<span></span></span></b></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;color: gray;'>YunXiaoDaSha Plaza NO.89 YuXiaoLu 3 St.BaiYun District,GuangZhou</span></b><span style='color: navy;'></span></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;color: gray;'>电话</span></b><b><span style='font-size: 9.0pt;font-family: arial , sans-serif;color: gray;'>: 020-66836611&nbsp;&nbsp;&nbsp; 36700150&nbsp;&nbsp;&nbsp; </span></b></p><p class='MsoNormal' style='margin: 0.0cm 0.0cm 0.0pt;'><b><span style='font-size: 9.0pt;font-family: arial , sans-serif;color: gray;'></span></b><b><span style='font-size: 9.0pt;color: gray;'>官方网站</span></b><b><span style='font-size: 9.0pt;font-family: arial , sans-serif;color: gray;'>: </span></b><span style='color: navy;'><a href='http://www.foreveross.com/' target='_blank'><b><span style='font-size: 9.0pt;font-family: arial , sans-serif;color: gray;'>http://www.foreveross.com/</span></b></a></span><b><span style='font-size: 9.0pt;font-family: arial , sans-serif;color: gray;'></span></b></p></td></tr></tbody></table></div></div></span></div>";
		mail({
			from: "gz-hr@foreveross.com", // sender address
			to: "guangzhou@foreveross.com", // list of receivers
			subject: "人事管理系统提醒", // Subject line
			html:sendMsg ,// html body
			attachments: attachments
		});
	}


	function handleDisconnect() { //mysql断线重连 
		connection.connect(function(err) {
			if (err) {
				console.log('error when connecting to db:', err);
				setTimeout(handleDisconnect, 2000);
			}
		});
		connection.on('error', function(err) {
			console.log('db error', err);
			if (err.code === 'PROTOCOL_CONNECTION_LOST') {
				handleDisconnect();
			} else {
				throw err;
			}
		});
	}
});


http.createServer(app).listen(app.get('port'), function() {
	console.log('Express server listening on port ' + app.get('port'));
});
exports = module.exports = app;